MyDesktop.Crontab = Ext.extend(Ext.app.Module, {

    init : function(){
        this.launcher = {
            text: _('Crontab'),
            iconCls:'cron',
            handler : this.createWindow,
            windowId : 'crontab',
            scope: this
        }
    },

    createWindow : function(src){
        var desktop = this.app.getDesktop();
        var winname = src.windowId;
        var win = desktop.getWindow(winname);

        if(!win){

        	var cron = Ext.data.Record.create([
        			{name: 'id'},
	            	{name: 'minute'},
	                {name: 'hour'},
	                {name: 'dom'},
	                {name: 'month'},
	                {name: 'dow'},
					{name: 'user'},
					{name: 'cmd'}
		     ]);

   			var ds = new Ext.data.Store({
				proxy: new Ext.data.HttpProxy({
					url: getMainUrl(),
					method: 'POST'
					}),
					baseParams: {action: 'crontab'},
				reader: new Ext.data.JsonReader({
						root: 'results',
						id: 'id'
					},
					cron)
			});
			
			var cm = new Ext.grid.ColumnModel({
				getCellEditor: function(colIndex, rowIndex) {
					return new Ext.grid.GridEditor(new Ext.form.TextField({allowBlank: false}));
				},
				columns: [{
						header: _('Minute'),
						editable: true,
						dataIndex: 'minute'
					},{
						header: _('Hour'),
						editable: true,
						dataIndex: 'hour'
					},{
						header: _('Day of Month'),
						editable: true,
						dataIndex: 'dom'
					},{
						header: _('Month'),
						editable: true,
						dataIndex: 'month'
					},{
						header: _('Day of Week'),
						dataIndex: 'dow',
						editable: true
					},{
						header: _('User'),
						dataIndex: 'user',
						hidden: true
					},{
						header: _('Command'),
						editable: true,
						dataIndex: 'cmd'
					}]
				}
			);
			
			cm.defaultSortable = true;

			var grid = new Ext.grid.EditorGridPanel({
		        store: ds,
		        clicksToEdit:1,
		        cm: cm,
		        viewConfig: {
		        	autoFill: true
		        },
		        tbar: [
			        {
			            text: _('Add'),
			            iconCls: 'add',
			            handler : function(){
			                var p = new cron({
			                	id: '',
				            	minute: '',
				            	hour: '',
				            	dom: '',
				            	month: '',
				            	dow: '',
				            	user: '',
				            	cmd: ''
							});
			                grid.stopEditing();
			                ds.insert(0, p);
			                grid.startEditing(0, 0);
			            }
			        },
			        {
			            text: _('Save'),
			            iconCls: 'save',
			            handler : function(){
			            	var records = ds.getModifiedRecords();
							if (records.length>0) {
							  	var util = new Verbum.Utilities();
								util.request({
									el: grid.getEl(),
									msg: _('Saving...'),
						            params: { 
						               action: "crontab", 
						               data: jsonArray(records),
						               op: 'save'
						              }											
								});
								
								util.on({
									'done': function() {
										ds.commitChanges();
										grid.view.refresh();
										//win.close();
									}
								})								
							} else {
								console.log("nothing to do");
							}
			            }
			        },
			        {
			            text: _('Reload'),
			            iconCls: 'x-tbar-loading',
			            handler : function(){
						  	var util = new Verbum.Utilities();
							util.request({
					            params: { 
					               action: "crontab", 
					               op: 'reload'
					              }											
							});
			            }
			        },
				]	
			});

            win = desktop.createWindow({
				id: winname,
				title: src.text,
				closable:true,
				width:450,
				height:350,
				plain:true,
				layout: 'fit',
				iconCls: 'cron',
				items: grid
            });
			
            ds.load();

        }
          
        win.show();
    },
	display : function(){
        this.createWindow();
	}
});
